#include "features2d-test.h"




using namespace std;  
using namespace cv; 


int templateMatching(){
	maderfaker::ImgShower im;
	Mat center, shifted, tmpl, result;
	int cols,rows, resultRows, resultCols, matchMethod = CV_TM_SQDIFF;
	double minVal, maxVal;
	Point minLoc, maxLoc,matchLoc;
	ofstream outFile;
	
	center	= imread(TCZEW_BRIDGE_CENTER	, CV_LOAD_IMAGE_GRAYSCALE);
	shifted = imread(TCZEW_BRIDGE_SHIFTED	, CV_LOAD_IMAGE_GRAYSCALE);
	im.sh("shifted", shifted);
	
	cols = center.cols;
	rows = center.rows;
	tmpl= Mat(center, Range(0, rows/2), Range(cols-50, cols));//Range(0,1));

	resultRows = rows - tmpl.rows +1;
	resultCols = cols - tmpl.cols +1;

	
	outFile.open(THIS_DIR + string("features2d-test.txt"), ios::out | ios::app);
		

	for(matchMethod=0;matchMethod<6;matchMethod++){
		result.create(resultRows, resultCols, CV_32FC1);

		matchTemplate(shifted, tmpl, result, matchMethod);
		normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());
		im.sh("normalized", result);
	
		minMaxLoc(result ,&minVal, &maxVal, &minLoc, &maxLoc,Mat());

		if(matchMethod == CV_TM_SQDIFF || matchMethod == CV_TM_SQDIFF_NORMED){
			matchLoc = minLoc;
		}
		else{
			matchLoc = maxLoc;
		}

	
		//imwrite(THIS_DIR + string("tmpl.png"), tmpl);
	
		char * tmpstr = new char[25];
		outFile << "metchod: "<<matchMethod; 
		
		outFile << "\tx: "<<matchLoc.x; 
		outFile	<< "\ty: " << matchLoc.y;
		sprintf(tmpstr, "%.20f", minVal);
		outFile	<< "\tminVal: " << tmpstr;
		sprintf(tmpstr, "%.20f", maxVal);
		outFile	<< "\tmaxVal: " << tmpstr<<endl;

	}

	outFile.close();
	waitKey();
	return 0;
}

